package com.example.back.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.back.dto.req.ZDrugDoDisableReq;
import com.example.back.dto.req.ZDrugListReq;
import com.example.back.dto.req.ZDrugSave;
import com.example.back.dto.resp.ZDrugInfoRsp;
import com.example.back.dto.resp.ZDrugListRsp;
import com.example.back.feign.FileFeign;
import com.example.back.jpa.BaseInfoRepository;
import com.example.back.mapper.ZDrugMapper;
import com.example.back.service.ZDrugService;
import com.example.common.entity.BaseInfo;
import com.example.common.support.Result;
import com.example.common.utils.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.javers.spring.annotation.JaversAuditable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @Author wxy
 * @Date 2021/1/15 13:53
 * @Version 1.0
 */
@Service
public class ZDrugServiceImpl implements ZDrugService {

    @Resource
    ZDrugMapper zDrugMapper;


    @Resource
    BaseInfoRepository baseInfoRepository;

    @Resource
    FileFeign fileFeign;
    /**
     * 中药列表
     */
    @Override
    public PageInfo<ZDrugListRsp> List(ZDrugListReq zDrugListReq) {
        return PageHelper.startPage(zDrugListReq.getPage(),zDrugListReq.getLimit()).doSelectPageInfo(() -> zDrugMapper.List(zDrugListReq));

    }

    /**
     * 启用、禁用
     */
    @Override
    @JaversAuditable
    public Object DoDisable(ZDrugDoDisableReq zDrugDoDisableReq) {

        BaseInfo baseInfo =baseInfoRepository.findById(zDrugDoDisableReq.getDID()).orElse(null);
        if (baseInfo.getDisable()){
            baseInfo.setDisable(false);
            baseInfoRepository.save(baseInfo);
        }else {
            baseInfo.setDisable(true);
            baseInfoRepository.save(baseInfo);
        }

        return null;
    }

    /**
     * 详情
     */
    @Override
    public ZDrugInfoRsp Info(ZDrugDoDisableReq zDrugDoDisableReq) {
        ZDrugInfoRsp zDrugInfoRsp = zDrugMapper.Info(zDrugDoDisableReq.getDID());
        return zDrugInfoRsp;
    }

    /**
     * 上传文件
     */
    @Override
    public HashMap<String,Object> UpLoad(MultipartFile file) {
        Result<Object> result = fileFeign.updateFile(file);        
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("UpSess",true);
        hashMap.put("url",result.getData());
        return hashMap;
    }

    /**
     *
     */
    @Override
    @JaversAuditable
    public Object Save(ZDrugSave zDrugSave) {

        BaseInfo baseInfoDb = baseInfoRepository.findById(zDrugSave.getDrugID()).orElse(null);
        if (ObjectUtils.isEmpty(baseInfoDb)){
            //保存
            BaseInfo baseInfo = new BaseInfo();
            baseInfo.setDrugID(IdUtils.generateId());
            baseInfo.setCreateTime(new Date());
            baseInfo.setImg(zDrugSave.getImg());
            baseInfo.setDrugName(zDrugSave.getDrugName());
            baseInfo.setPrice(zDrugSave.getPrice());
            baseInfo.setPy(zDrugSave.getPY());
            baseInfo.setDisable(true);
            baseInfoRepository.save(baseInfo);
        }else {
            baseInfoDb.setImg(zDrugSave.getImg());
            baseInfoDb.setDrugName(zDrugSave.getDrugName());
            baseInfoDb.setPrice(zDrugSave.getPrice());
            baseInfoDb.setPy(zDrugSave.getPY());
            baseInfoRepository.save(baseInfoDb);
        }

        return null;
    }
}
